package crawler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


import crawler.naive.TrovaCasaNaiveCrawler;
import crawler.naive.TrovaCasaThread;

public class TrovaCaseAdapter implements IAnnouncementCrawler {

	private File _destination;
	public TrovaCaseAdapter(String path) {
		_destination = new File(path+"/trovacase");
	}

	@Override
	public void crawl() {
		int group = 1081;
		long steps = 45;
		StatisticsFileWriter.getInstance().register("http://www.trova-casa.net/",(steps+1)*200*group);
		for(int j=0; j<=steps; j++){			
			int begin = j*200;
			int end = (j+1)*200;
			List<TrovaCasaThread> threads = new ArrayList<TrovaCasaThread>();
			for(int i=begin; i<=end; i++){
				long minIndex = group*i;
				long maxIndex = (group*(i+1)-1);
				TrovaCasaNaiveCrawler crawler = new TrovaCasaNaiveCrawler(minIndex,maxIndex);
				TrovaCasaThread thread = new TrovaCasaThread(crawler,_destination);
				thread.start();
				threads.add(thread);
			}
			for (TrovaCasaThread each : threads) {
				try {
					each.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
